# -*- coding: utf-8 -*-
# @Time : 2021/7/25 10:03
# @Author : zy7y
# @Gitee : https://gitee.com/zy7y
# @File : BasePage.py
# @Project : SeleniumPOM
from typing import Tuple

from pydantic import HttpUrl
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


"""
基础的页面对象,封装常用的页面方法
"""

# 类型别名
Locator = Tuple[By, str]


class BasePage:

    def __init__(self, driver: webdriver.Chrome):
        """
        初始化方法
        :param driver: 浏览器driver对象
        """
        self.driver = driver
        self.seconds = 10

    def do_find_element(self, by_locator: Locator) -> WebElement:
        """
        动作 > 显示等待 -> 查找元素
        :param by_locator: (定位方式,定位元素)
        :return:
        """
        return WebDriverWait(
            self.driver, self.seconds).until(
            EC.presence_of_element_located(by_locator))

    def do_visit(self, url: HttpUrl):
        """
        动作 > 访问页面
        :param url: 页面路径
        :return: None
        """
        self.driver.get(url)

    def do_click(self, by_locator: Locator):
        """
        动作 > 点击元素
        :param by_locator: (定位方式,定位元素)
        :return: None
        """
        self.do_find_element(by_locator).click()

    def do_input(self, by_locator: Locator, text: str):
        """
        动作 > 输入内容
        :param by_locator:  (定位方式,定位元素)
        :param text: 输入内容
        :return: None
        """
        self.do_find_element(by_locator).send_keys(text)

    def get_element_text(self, by_locator: Locator) -> str:
        """
        获取元素文本
        :param by_locator: (定位方式,定位元素)
        :return: 文本内容
        """
        return self.do_find_element(by_locator).text

    def is_enabled(self, by_locator: Locator) -> bool:
        """
        元素是否存在
        :param by_locator: (定位方式,定位元素)
        :return: [True | False]
        """
        return bool(self.do_find_element(by_locator))

    def get_title(self) -> str:
        """
        获取页面标题
        :return:
        """
        return self.driver.title


